const fs = require('fs');
const path = require('path');
const zh = require('D:\\桌面\\新建文件夹\\src\\zh.js')
const newZh = require('D:\\gitee项目\\微前端调度台\\report\\src\\lang\\zh.json')
let json = {}
let repeat = []
function readFilesRecursively(folderPath) {
  const files = fs.readdirSync(folderPath);
  files.forEach(file => {
    const filePath = path.join(folderPath, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      readFilesRecursively(filePath);
    } else if (stats.isFile()) {
      const list = filePath.split('.')
      const suffix = list[list.length - 1]
      if (suffix === 'vue') {
        // 在这里处理文件
        setFile(filePath)
      } else if (suffix === 'js') {
        setFile(filePath)
      }
    }
  });
}
function findKeyByValue(obj, value) {
  return Object.keys(obj).find(key => obj[key] === value);
}
function setTemplate (initContent) {
  let text = ''
  const lines = initContent.split(/\r?\n/);
  lines.forEach((line, index) => {
    if (line.includes('console.') || line.includes('//') || line.includes('/*') || line.includes('* ') || line.includes('<!--')) {
      text += '\n' + line
      return
    }
    line = line.replace(/'(\w+)\.(\w+)'/g, (_,a,b) => {
      const item = zh[`${a}`]
      if (item) {
        let row = item[`${b}`] + ''
        const res = findKeyByValue(newZh, row)
        if (res) {
          return `'${res}'`
        }
      }
      return _
    })
    text += '\n' + line
  });
  return text
}

function setFile (path) {
  fs.readFile(path, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }
    setWriteFile(path,  setTemplate(data))
  });
}

function setWriteFile (path, data) {
  fs.writeFile(path, data, 'utf8', (err) => {
    if (err) {
      console.error(err);
      return;
    }
    // console.log('执行成功')
  });
}
const folderPath = 'D:\\gitee项目\\微前端调度台\\report\\src';
// const folderPath = 'D:\\gitee项目\\微前端调度台\\新建文件夹\\command-main\\src';
readFilesRecursively(folderPath);
